package haoyou.csnf.manager.module.pm.controller.admin.employeeinfo;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import haoyou.csnf.manager.framework.common.pojo.PageParam;
import haoyou.csnf.manager.framework.common.pojo.PageResult;
import haoyou.csnf.manager.framework.common.pojo.CommonResult;
import haoyou.csnf.manager.framework.common.util.object.BeanUtils;
import static haoyou.csnf.manager.framework.common.pojo.CommonResult.success;

import haoyou.csnf.manager.framework.excel.core.util.ExcelUtils;

import haoyou.csnf.manager.framework.apilog.core.annotation.ApiAccessLog;
import static haoyou.csnf.manager.framework.apilog.core.enums.OperateTypeEnum.*;

import haoyou.csnf.manager.module.pm.controller.admin.employeeinfo.vo.*;
import haoyou.csnf.manager.module.pm.dal.dataobject.employeeinfo.EmployeeInfoDO;
import haoyou.csnf.manager.module.pm.dal.dataobject.employeeeducations.EmployeeEducationsDO;
import haoyou.csnf.manager.module.pm.dal.dataobject.employeetitles.EmployeeTitlesDO;
import haoyou.csnf.manager.module.pm.service.employeeinfo.EmployeeInfoService;

@Tag(name = "管理后台 - 职工信息")
@RestController
@RequestMapping("/pm/employee-info")
@Validated
public class EmployeeInfoController {

    @Resource
    private EmployeeInfoService employeeInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建职工信息")
    @PreAuthorize("@ss.hasPermission('pm:employee-info:create')")
    public CommonResult<Long> createEmployeeInfo(@Valid @RequestBody EmployeeInfoSaveReqVO createReqVO) {
        return success(employeeInfoService.createEmployeeInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新职工信息")
    @PreAuthorize("@ss.hasPermission('pm:employee-info:update')")
    public CommonResult<Boolean> updateEmployeeInfo(@Valid @RequestBody EmployeeInfoSaveReqVO updateReqVO) {
        employeeInfoService.updateEmployeeInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除职工信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pm:employee-info:delete')")
    public CommonResult<Boolean> deleteEmployeeInfo(@RequestParam("id") Long id) {
        employeeInfoService.deleteEmployeeInfo(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除职工信息")
                @PreAuthorize("@ss.hasPermission('pm:employee-info:delete')")
    public CommonResult<Boolean> deleteEmployeeInfoList(@RequestParam("ids") List<Long> ids) {
        employeeInfoService.deleteEmployeeInfoListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得职工信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pm:employee-info:query')")
    public CommonResult<EmployeeInfoRespVO> getEmployeeInfo(@RequestParam("id") Long id) {
        EmployeeInfoDO employeeInfo = employeeInfoService.getEmployeeInfo(id);
        return success(BeanUtils.toBean(employeeInfo, EmployeeInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得职工信息分页")
    @PreAuthorize("@ss.hasPermission('pm:employee-info:query')")
    public CommonResult<PageResult<EmployeeInfoRespVO>> getEmployeeInfoPage(@Valid EmployeeInfoPageReqVO pageReqVO) {
        PageResult<EmployeeInfoDO> pageResult = employeeInfoService.getEmployeeInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, EmployeeInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出职工信息 Excel")
    @PreAuthorize("@ss.hasPermission('pm:employee-info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportEmployeeInfoExcel(@Valid EmployeeInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<EmployeeInfoDO> list = employeeInfoService.getEmployeeInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "职工信息.xls", "数据", EmployeeInfoRespVO.class,
                        BeanUtils.toBean(list, EmployeeInfoRespVO.class));
    }

    // ==================== 子表（职工学历） ====================

    @GetMapping("/employee-educations/page")
    @Operation(summary = "获得职工学历分页")
    @Parameter(name = "empId", description = "职工ID")
    @PreAuthorize("@ss.hasPermission('pm:employee-info:query')")
    public CommonResult<PageResult<EmployeeEducationsDO>> getEmployeeEducationsPage(PageParam pageReqVO,
                                                                                        @RequestParam("empId") Integer empId) {
        return success(employeeInfoService.getEmployeeEducationsPage(pageReqVO, empId));
    }

    @PostMapping("/employee-educations/create")
    @Operation(summary = "创建职工学历")
    @PreAuthorize("@ss.hasPermission('pm:employee-info:create')")
    public CommonResult<Long> createEmployeeEducations(@Valid @RequestBody EmployeeEducationsDO employeeEducations) {
        return success(employeeInfoService.createEmployeeEducations(employeeEducations));
    }

    @PutMapping("/employee-educations/update")
    @Operation(summary = "更新职工学历")
    @PreAuthorize("@ss.hasPermission('pm:employee-info:update')")
    public CommonResult<Boolean> updateEmployeeEducations(@Valid @RequestBody EmployeeEducationsDO employeeEducations) {
        employeeInfoService.updateEmployeeEducations(employeeEducations);
        return success(true);
    }

    @DeleteMapping("/employee-educations/delete")
    @Parameter(name = "id", description = "编号", required = true)
    @Operation(summary = "删除职工学历")
    @PreAuthorize("@ss.hasPermission('pm:employee-info:delete')")
    public CommonResult<Boolean> deleteEmployeeEducations(@RequestParam("id") Long id) {
        employeeInfoService.deleteEmployeeEducations(id);
        return success(true);
    }

    @DeleteMapping("/employee-educations/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除职工学历")
    @PreAuthorize("@ss.hasPermission('pm:employee-info:delete')")
    public CommonResult<Boolean> deleteEmployeeEducationsList(@RequestParam("ids") List<Long> ids) {
        employeeInfoService.deleteEmployeeEducationsListByIds(ids);
        return success(true);
    }

	@GetMapping("/employee-educations/get")
	@Operation(summary = "获得职工学历")
	@Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pm:employee-info:query')")
	public CommonResult<EmployeeEducationsDO> getEmployeeEducations(@RequestParam("id") Long id) {
	    return success(employeeInfoService.getEmployeeEducations(id));
	}

    // ==================== 子表（职工职称） ====================

    @GetMapping("/employee-titles/page")
    @Operation(summary = "获得职工职称分页")
    @Parameter(name = "empId", description = "职工ID")
    @PreAuthorize("@ss.hasPermission('pm:employee-info:query')")
    public CommonResult<PageResult<EmployeeTitlesDO>> getEmployeeTitlesPage(PageParam pageReqVO,
                                                                                        @RequestParam("empId") Integer empId) {
        return success(employeeInfoService.getEmployeeTitlesPage(pageReqVO, empId));
    }

    @PostMapping("/employee-titles/create")
    @Operation(summary = "创建职工职称")
    @PreAuthorize("@ss.hasPermission('pm:employee-info:create')")
    public CommonResult<Long> createEmployeeTitles(@Valid @RequestBody EmployeeTitlesDO employeeTitles) {
        return success(employeeInfoService.createEmployeeTitles(employeeTitles));
    }

    @PutMapping("/employee-titles/update")
    @Operation(summary = "更新职工职称")
    @PreAuthorize("@ss.hasPermission('pm:employee-info:update')")
    public CommonResult<Boolean> updateEmployeeTitles(@Valid @RequestBody EmployeeTitlesDO employeeTitles) {
        employeeInfoService.updateEmployeeTitles(employeeTitles);
        return success(true);
    }

    @DeleteMapping("/employee-titles/delete")
    @Parameter(name = "id", description = "编号", required = true)
    @Operation(summary = "删除职工职称")
    @PreAuthorize("@ss.hasPermission('pm:employee-info:delete')")
    public CommonResult<Boolean> deleteEmployeeTitles(@RequestParam("id") Long id) {
        employeeInfoService.deleteEmployeeTitles(id);
        return success(true);
    }

    @DeleteMapping("/employee-titles/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除职工职称")
    @PreAuthorize("@ss.hasPermission('pm:employee-info:delete')")
    public CommonResult<Boolean> deleteEmployeeTitlesList(@RequestParam("ids") List<Long> ids) {
        employeeInfoService.deleteEmployeeTitlesListByIds(ids);
        return success(true);
    }

	@GetMapping("/employee-titles/get")
	@Operation(summary = "获得职工职称")
	@Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pm:employee-info:query')")
	public CommonResult<EmployeeTitlesDO> getEmployeeTitles(@RequestParam("id") Long id) {
	    return success(employeeInfoService.getEmployeeTitles(id));
	}

}